package com.wenhai.spring.aop.before;

import com.wenhai.spring.aop.Guiatrist;
import org.springframework.aop.BeforeAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.lang.NonNull;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * {@link BeforeAdvice} 示例 基于{@link MethodBeforeAdvice}
 *
 * @author wenhai
 * @date   2021/5/5
 * @see BeforeAdvice
 * @see MethodBeforeAdvice
 */
public class SimpleBeforeAdvice implements MethodBeforeAdvice {

    public static void main(String[] args) {
        Guiatrist guiatrist = new Guiatrist();
        ProxyFactory pf = new ProxyFactory();
        pf.addAdvice(new SimpleBeforeAdvice());
        pf.setTarget(guiatrist);
        Guiatrist proxy = (Guiatrist) pf.getProxy();
        proxy.sing("文海");
    }


    @Override
    public void before(@NonNull Method method, @NonNull Object[] args, Object target) throws Throwable {
        System.out.println(Arrays.toString(args));

        args[0] = "谢文海";
        System.out.println("Before '" + method.getName() + "', tune guitar.");
    }
}
